import { defineStore } from "pinia";
import { ref } from "vue";

const key: string = "useKeepAliveStore";

export const useKeepAliveStore = defineStore(key, () => {
  /**
   * @description 需要缓存组件的名称
   */
  const keepAliveName = ref<string[]>([]);

  /**
   * @description 添加缓存组件
   * @param name 组件名称
   */
  function addKeepAliveName(name: string) {
    !keepAliveName.value.includes(name) && keepAliveName.value.push(name);
  }

  /**
   * @description 移除缓存组件
   * @param name 组件名称
   */
  function removeKeepAliveName(name: string) {
    keepAliveName.value = keepAliveName.value.filter((item) => item !== name);
  }

  /**
   * @description 设置缓存组件
   * @param keepAliveNameList 组件名称列表
   */
  function setKeepAliveName(keepAliveNameList: string[] = []) {
    keepAliveName.value = keepAliveNameList;
  }

  return {
    keepAliveName,
    addKeepAliveName,
    removeKeepAliveName,
    setKeepAliveName
  };
});
